<template>
  <el-col class="commodity">
    <el-col class="title">评论列表</el-col>

    <el-col class="list_table">
      <el-table :data="commodity_list" border stripe size="small">
        <el-table-column
          v-for="col in columns"
          :prop="col.id"
          :key="col.id"
          :label="col.label"
          :width="col.width"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="评论图片">
          <template slot-scope="scope">
            <template v-for="(item,index) in scope.row.images">
              <span class="imags" :key="index" @click="showMdel(item)">
                <img :src="item" alt srcset />
              </span>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="商品状态" width="120">
          <template slot-scope="scope">
            <template v-if="scope.row.status===0">已下架</template>
            <template v-else>在售</template>
          </template>
        </el-table-column>
        <el-table-column label="评论状态" width="80">
          <template slot-scope="scope">
            <template v-if="scope.row.status===0">禁用</template>
            <template v-else>正常</template>
          </template>
        </el-table-column>
        <el-table-column label="操 作" min-width="220">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click.native.prevent="check(scope.$index,scope.row)"
              type="warning"
            >
              <i class="el-icon-edit-outline"></i>
              回复
            </el-button>
            <template v-if="scope.row.status==1">
              <el-button
                @click.native.prevent="editRow(scope.$index,scope.row)"
                type="primary"
                size="mini"
              >
                <i class="el-icon-edit-outline"></i>
                禁用
              </el-button>
            </template>
            <template v-else>
              <el-button
                @click.native.prevent="editRow(scope.$index,scope.row)"
                type="success"
                size="mini"
              >
                <i class="el-icon-edit-outline"></i>
                启用
              </el-button>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </el-col>
    <el-col class="page">
      <!-- <el-col :span="5">
        <el-button type="primary" size="mini" @click="addlist">添加商品</el-button>
      </el-col>-->
      <el-pagination
        ref="pagination"
        layout="prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
        :page-size="12"
        class="page_center"
      ></el-pagination>
    </el-col>
    <el-col class="dg">
      <el-dialog
        title="评论回复"
        :visible.sync="dialog"
        width="960px"
        @close="end"
        :modal-append-to-body="false"
      >
        <el-form :model="form" ref="form" label-width="80px" class="elf">
          <el-form-item label="回复评论">
            <el-input v-model="form.comment" type="textarea" :rows="3" placeholder="请输入回复评论内容"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="commentdata">立即回复</el-button>
            <!-- <el-button>取消</el-button> -->
          </el-form-item>
        </el-form>

        <span slot="footer">
          &nbsp;
          <!-- <el-button type="primary" @click="">确 定</el-button> -->
        </span>
      </el-dialog>
    </el-col>
    <el-dialog :visible.sync="el_dialog_show" :append-to-body="true" @close="end">
      <div>
        <img :src="this.dialogimg" alt srcset />
      </div>
      <span slot="footer">
        <el-button @click=" el_dialog_show= false">关闭</el-button>
        <!-- <el-button type="primary" @click="">确 定</el-button> -->
      </span>
    </el-dialog>
  </el-col>
</template>
<script>
export default {
  name: "commodity",
  data() {
    return {
      el_dialog_show: false,
      form: {
        comment: "" //回复评论参数
      },
      product_id: "",
      screen: {
        name: "",
        region: "",
        page: "",
        status: ""
      },
      page: "",
      commodity_list: [],
      columns: [
        {
          id: "com_id",
          label: "商品序号",
          width: "80"
        },
        {
          id: "com_name",
          label: "商品名称"
        },
        {
          id: "content",
          label: "评论内容",
          width: ""
        },

        {
          id: "user_name",
          label: "评论者"
        },
        {
          id: "created_at",
          label: "评论时间"
        }
      ],
      total: 0,
      dialog: false,
      datadetail: ""
    };
  },
  mounted() {
    this.getlist();
  },
  methods: {
    showMdel(path) {
      //path
      this.el_dialog_show = true;
      this.dialogimg = path;
    },
    async getlist() {
      //获取商品列表
      let res = await this.$store.dispatch(
        "commodity_review_list",
        this.screen
      );
      if (!res.code) {
        res.data.data.map((item, index) => {
          if (item.images) {
            res.data.data[index].images = JSON.parse(item.images);
          }
        });
        this.commodity_list = res.data.data;
        console.log(this.commodity_list);
        this.total = res.data.total;
      }
    },
    screenfn() {
      //筛选状态
      this.page = "";
      this.$refs.pagination.internalCurrentPage = 1;
      this.getlist();
    },
    async commentdata() {
      //回复评论
      let data = {
        product_id: this.product_id,
        content: this.form.comment
      };
      let res = await this.$store.dispatch("reply_product", data);
      if (!res.code) {
        this.$message({
          type: "success",
          message: "回复成功"
        });
        this.dialog = false;

        this.getlist();
      }
    },
    async editRow(index, row) {
      //评论禁用
      console.log(index, row);
      // product_id
      let data = {
        product_id: row.id
      };
      let res = await this.$store.dispatch("disable_product", data);
      if (row.status == 0) {
        this.$message({
          message: "启用成功",
          type: "success"
        });
      } else {
        this.$message({
          message: "禁用成功",
          type: "error"
        });
      }
      if (!res.code) {
        this.getlist();
      }

      //   this.$router.push({
      //     path: "/home/addcommdity",
      //     query: {
      //       id: row.id
      //     }
      //   });
    },
    async check(index, row) {
      //评论回复
      // console.log(index, row);
      this.product_id = row.id;
      this.dialog = true;

      //   let data = {
      //     com_id: row.id
      //   };
      //   let res = await this.$store.dispatch("getdetail_commodity", data);
      //   this.datadetail = res.data;
    },
    handleCurrentChange(val) {
      this.screen.page = val;
      this.getlist();
    },
    addlist() {
      //商品列表添加
      this.$router.push({
        path: "/home/addcommdity"
      });
    },
    end() {
      //清空商品信息
      this.dialog = false;
      this.el_dialog_show = false;
      this.dialogimg = "";
    }
  }
};
</script>
<style lang="scss" scoped>
.elf {
  padding-top: 30px;
}
.imags {
  display: inline-block;
  width: 40px;
  height: 40px;
  overflow: hidden;
  margin: 0 5px;
  cursor: pointer;
  img {
    width: 40px;
    height: 40px;
  }
}
</style>